/**
 * @description 系统首页js-systemIndex
 * @author guoyanfei
 * @date 2017/12/11
 */
//项目路径
var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = "/gdp";
var systemIndex = systemIndex || {};
systemIndex.prototype={
		init:function(){//初始化
			systemIndex.prototype.getProjectList();
			systemIndex.prototype.closeMoal();
		},
		getProjectList:function(){//查询项目列表
			$.ajax({
				type:'POST',
				url:postPath+'/system/index/getAllProject',
				success:function(data){
					var projectList="";
					$.each(data,function(i,e){
						//拼接项目信息
						projectList += '<div class="col-xs-12" style="width:350px;">';
						projectList +='<div class="mt-element-ribbon bg-grey-steel">';
						projectList +='<div class="ribbon ribbon-border-hor ribbon-clip ribbon-shadow ribbon-round ribbon-color-danger uppercase">';
						projectList +='<div class="ribbon-sub ribbon-clip"></div> '+e.pname+' ';
						projectList +='</div>';
						projectList +='<div title="已配置服务" class=" numberCli bg-blue font-white"><a href="/prjService/index?a_pcode='+e.pcode+'" style="color:#FFF;">'+e.servicesnum+'</a></div>';
						projectList +='<div class="ribbon-content font-grey-cascade" style="padding-bottom:40px;">';
						projectList +='<div title="'+e.pdesc+'">项目说明：'+(e.pdesc.length>13 ? e.pdesc.substring(0,13)+"...":e.pdesc )+'</div>';
						projectList +='<div>备注：'+e.remark+'</div>';
						projectList +='</div>';
						projectList +='<div class="importData">';
						projectList +='<button class="btn blue-steel" onclick="systemIndex.prototype.exportProjectData(\''+e.pcode+'\',\''+e.pname+'\')"> 导出数据 <i class="fa fa-expand"></i></button>';
						projectList +='</div></div></div>';
						
					});
					$("#projectList").append(projectList);
				}
			});
		},
		exportProjectData:function(pcode,pname){//导出数据
			bootbox.confirm("确认导出项目:"+pname+"的数据吗?", function(result) {
				if(!result){
					return;
				}
				$("#exportData").modal('show');
				var Isprogress = setTimeout(systemIndex.prototype.getProgressInfo(false),500);
				
				$.ajax({
					type:'POST',
					url:postPath+'/system/index/exportProjectData',
					data:{pcode:pcode},
					success:function(data){
						clearTimeout(Isprogress);
						if(data !=""){
                            systemIndex.prototype.getProgressInfo(true);
							$("#btn_exportClose").css("display","");
							$("#a_download").attr("href",postPath+'/system/index/downloadData?filename='+data);
							$("#a_download").css("display","");
						}else{
							$("#logcontent").append("导出失败,请刷新后重试...<br/>");
							$("#btn_exportClose").css("display","");
						}
						
					}
				});
				
			});
		},
	    importProjectData:function(formData){
            //$("#implogcontent").remove();
            bootbox.confirm("确认导入数据吗?", function(result) {
                if(!result){
                    return;
                }
                $("#importData").modal('show');
                $.ajax({
                    type:'POST',
                    url:postPath+'/system/index/importData',
                    data:formData,
                	async : false,
                	processData : false,
                	contentType : false,
                	success:function(data){
                    	if(data =="sql导入成功"){

                            $("#implogcontent").html("");
                            $("#imppro").css("width","100%");
                            //$("#imppro").css("background-color","#337ab7");
                            $("#impsp_gro").html("");
                            $("#impsp_gro").html("100%");
                            $("#implogcontent").append("正在导入数据...<br/>");
                            $("#implogcontent").append(data);
                            $("#btn_importClose").css("display","");

                    	}else{
                            $("#implogcontent").html("");
                            $("#imppro").css("width","100%");
                            //$("#imppro").css("background-color","#e43a45");
                            $("#impsp_gro").html("");
                            $("#impsp_gro").html("100%");
                            $("#implogcontent").append("正在导入数据...<br/>");
                            $("#implogcontent").append(data);
                            $("#btn_importClose").css("display","");
                   	 	}

               	 	}
            	});
            });
		},
		getProgressInfo:function(issuccess){//获取进度条信息
			$.ajax({
				type:'POST',
				url:postPath+'/system/index/getProgressInfo',
				success:function(data){
					$("#logcontent").html(data.message);
					if(issuccess == false){
						$("#pro").css("width",data.ratio+"%");
						$("#sp_gro").html(data.ratio+"% 正在导出...");
					}else{
						$("#pro").css("width","100%");
						$("#sp_gro").html("100% 完成");
						$("#logcontent").append("导出完成...<br/>");
					}
					
				}
			});
		},
		closeMoal:function(){
			$("#exportData").on('hidden.bs.modal', function (e) {
				$("#pro").css("width","0%");
				$("#sp_gro").html("0% 正在导出...");
				$("#logcontent").html("正在导出...");
		    });
		}
};

$(function(){
	systemIndex.prototype.init();
})


